﻿using Abp.Domain.Repositories;
using Abp.UI;
using shenghua.Commodity.Commodities;
using shenghua.Common.WordNodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.ShoppingOptionParameters
{
    public class ShoppingOptionParameterManager : WordNodeManager<ShoppingOptionParameter>
    {
        private readonly IRepository<Commodity.Commodities.Commodity> _commodityRepository;

        public ShoppingOptionParameterManager(
            IRepository<ShoppingOptionParameter> commodityClassRepository,
            IRepository<Commodity.Commodities.Commodity> commodityRepository)
            : base(commodityClassRepository)
        {
            _commodityRepository = commodityRepository;
        }

        public override async Task Delete(int id)
        {
            ShoppingOptionParameter shoppingOptionParameter = await base.Get(id);

            // 检测是否有商品使用了该购物选项
            int count = _commodityRepository.GetAll().Where(r => 
                r.SelectedShoppingOptionParameters.Items.Contains(shoppingOptionParameter.Name)).Count();

            if(count > 0)
            {
                throw new UserFriendlyException($"存在使用{shoppingOptionParameter.Name}的商品");
            }
            
            await base.Delete(id);
        }
    }
}
